package cn.anc.aonicardv.module.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.OnClick;
import cn.anc.aonicardv.Constant;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.R2;
import cn.anc.aonicardv.module.ui.base.BaseActivity;
import cn.anc.aonicardv.module.ui.my.PrivacyActivity;
import cn.anc.aonicardv.util.ActivityUtils;
import cn.anc.aonicardv.util.SpUtils;

/**
 * Created by yangdai on 2018/1/17.
 */

public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    @BindView(R2.id.cb_guide)
    ConvenientBanner guideCb;
    @BindView(R2.id.tv_jump)
    TextView jumpTv;
    private List<Integer> localImages = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_guide);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initVariable() {
        guideCb.setOnPageChangeListener(this);
    }

    @Override
    public void setListener() {
    }

    @OnClick(R2.id.tv_jump)
    public void jump() {
       Boolean isAgreePrivacy =  SpUtils.getBoolean(Constant.SpKeyParam.IS_AGREE_PRIVACY,false);
        if (isAgreePrivacy){
            ActivityUtils.jumpActivity(this, MainActivity.class);
        }else{
            ActivityUtils.jumpActivity(this, PrivacyActivity.class);
        }
        finish();
        SpUtils.putBoolean(Constant.SpKeyParam.IS_LOAD_GUIDE_PAGE, false);
    }

    @Override
    public void initData() {
        localImages.add(R.mipmap.page_01);
        localImages.add(R.mipmap.page_02);
        localImages.add(R.mipmap.page_05);
        guideCb.setCanLoop(false);
        guideCb.setPointViewVisible(true);
        guideCb.setManualPageable(true);
        guideCb.setPages(
                new CBViewHolderCreator<LocalImageHolderView>() {
                    @Override
                    public LocalImageHolderView createHolder() {
                        return new LocalImageHolderView();
                    }
                }, localImages).setPageIndicator(new int[]{R.mipmap.circle_indicator_no_selected, R.mipmap.circle_indicator_selected})
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        if (i != 2) {
            jumpTv.setVisibility(View.VISIBLE);
        } else {
            jumpTv.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    public class LocalImageHolderView implements Holder<Integer>, View.OnClickListener {
        private ImageView guideIv;
        private TextView guideTv;
        private TextView nowExperienceTv;

        @Override
        public View createView(Context context) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_guide, guideCb, false);
            guideIv = view.findViewById(R.id.iv_guide);
            guideTv = view.findViewById(R.id.tv_guide);
            nowExperienceTv = view.findViewById(R.id.tv_now_experience);
            return view;
        }

        @Override
        public void UpdateUI(Context context, int position, Integer data) {
            if (position == 0) {
                nowExperienceTv.setVisibility(View.INVISIBLE);
                guideIv.setImageResource(R.mipmap.page_01);
                guideTv.setText(R.string.guide_hint1);
            } else if (position == 1) {
                nowExperienceTv.setVisibility(View.INVISIBLE);
                guideIv.setImageResource(R.mipmap.page_02);
                guideTv.setText(R.string.guide_hint2);
            } else if (position == 2) {
                nowExperienceTv.setVisibility(View.VISIBLE);
                guideIv.setImageResource(R.mipmap.page_05);
                guideTv.setText(R.string.guide_hint5);
            }
            nowExperienceTv.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            jump();
        }
    }
}
